package com.hejia.bulbul.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.hejia.alauda.entity.MediaInstitution;
import com.hejia.alauda.service.PageService;
import com.hejia.alauda.utils.Pager;

@Controller
@RequestMapping("safeguardmedium")
public class MediaInstitutionController extends BaseController {

	@Resource
	private PageService mediaInstitutionService;

	@RequestMapping(value = "safeguardmediumlist", method = RequestMethod.GET)
	public String safeguardMediumList(ModelMap model,
			Pager<MediaInstitution> pager) {
		Pager<MediaInstitution> safeguardmediums = mediaInstitutionService
				.findAllMediaInstitution(pager);
		model.put("safeguardmediums", safeguardmediums);
		return "/page/safeguardmedium_list";
	}

	/**
	 * 跳转到添加界面
	 */
	@RequestMapping(value = "safeguardmediumadd", method = RequestMethod.GET)
	public String addSafeguardMedium(ModelMap model) {

		return "/page/safeguardmedium_input";
	}

	/**
	 * 添加完保存
	 */
	@RequestMapping(value = "safeguardmediumadd", method = RequestMethod.POST)
	public String saveSafeguardMedium(MediaInstitution mediaInstitution,
			ModelMap model, RedirectAttributes redirectAttributes) {
		if (mediaInstitutionService.saveMediaInstitution(mediaInstitution)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "媒体机构添加成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "媒体机构添加失败");
		}

		return "redirect:/safeguardmedium/safeguardmediumlist";
	}

	/**
	 * 跳转到修改界面
	 */
	@RequestMapping(value = "safeguardmediumupdate/{id}", method = RequestMethod.GET)
	public String updateSafeguardMedium(@PathVariable Long id, ModelMap model) {
		MediaInstitution safeguardMedium = mediaInstitutionService
				.getMediaInstitutionById(id);
		model.put("safeguardmedium", safeguardMedium);

		return "/page/safeguardmedium_input";
	}

	/**
	 * 修改完保存
	 */
	@RequestMapping(value = "safeguardmediumupdate/{id}", method = RequestMethod.POST)
	public String saveUpdateSafeguardMedium(MediaInstitution mediaInstitution,
			RedirectAttributes redirectAttributes) {
		if (mediaInstitutionService.modifyMediaInstitution(mediaInstitution)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "媒体机构修改成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "媒体机构修改失败");
		}

		return "redirect:/safeguardmedium/safeguardmediumlist";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "safeguardmediumdelete/{id}", method = RequestMethod.GET)
	public String deleteMediaInstitution(@PathVariable Long id,
			RedirectAttributes redirectAttributes) {
		if (mediaInstitutionService.deleteMediaInstitution(id)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "媒体机构删除成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "媒体机构删除失败");
		}

		return "redirect:/safeguardmedium/safeguardmediumlist";
	}

}
